<template>
  <el-dialog append-to-body title="调整部门" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true" :visible.sync="visible" :width="'480px'">
    <el-form :model="myForm" label-width="100px" size="small" @submit.native.prevent>
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-form-item label="选择部门">
            <el-cascader size="small" placeholder="请选择选择部门" clearable filterable :options="depart" :props="{label: 'name', value: 'id', checkStrictly: true}" :disabled="disabled" v-model="myForm.parent" class="full-width"></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :disabled="submitLoading" @click="closeModel()">取消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="validForm()">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { UpdateUserDepart } from '@/views/user-management/services/index';
export default {
  props: ['visible', 'depart'],
  data() {
    return {
      idLists: [],
      myForm: {
        parent: null
      },
      disabled: false,
      submitLoading: false
    }
  },
  methods: {
    closeModel() {
      this.$emit('update:visible', false)
    },
    validForm() {
      if (!this.myForm.parent || !this.myForm.parent.length) {
        this.$message.warning('请选择部门!');
        return;
      }
      this.submitLoading = true;
      UpdateUserDepart({
        userIds: this.idLists,
        depId: this.myForm.parent[this.myForm.parent.length - 1]
      })
      .then(success => {
        this.submitLoading = false;
        if (success.code == 200) {
          this.$message.success('调整部门成功!');
          this.closeModel();
          this.resetForm();
          this.$emit('update', true);
        } else {
          this.$message.error('调整部门失败!');
        }
      })
      .catch(error => {
        this.submitLoading = false;
        this.$message.error(error);
      })
    },
    resetForm() {
      this.submitLoading = false;
      this.idLists = [];
      this.myForm.parent = null;
    }
  }
}
</script>
